package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.ApiMapping;
import com.qfedu.openplateform.manager.entity.ApiSystemparam;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.ApiSystemparamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 叶上秋
 * @date: 2021/10/27 17:54
 * @version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/sys/systemparaters")
public class ApiSystemparamController {
    @Autowired
    private ApiSystemparamService systemparamService;

    @RequestMapping("/table")
    public TableData findByPageApiSystemparam(@RequestParam(value = "page",defaultValue = "1",required = false) Integer pageNum,
                                          @RequestParam(value = "limit",defaultValue = "10",required = false) Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        List<ApiSystemparam> allApiSystemparam = systemparamService.findAllApiSystemparam();
        PageInfo<ApiSystemparam> pageInfo = new PageInfo<>(allApiSystemparam);

        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("/add")
    public void addApiMapping(ApiSystemparam apiSystemparam){

        systemparamService.addApiSystemparam(apiSystemparam);
    }

    /**
     * 根据id删除路由
     * @param ids 需要删除的路由ids
     * @return
     */
    @RequestMapping("/del")
    public AjaxMessage deleteApiMapping(Integer[] ids){
        try {
            systemparamService.deleteApiSystemparam(ids);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(true, "删除失败");
        }
    }

    @RequestMapping("/update")
    public AjaxMessage update(ApiSystemparam apiSystemparam) {
        try {
            systemparamService.updateApiSystemparam(apiSystemparam);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(true, "修改失败");
        }
    }
}
